package crmsystem.com.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import crmsystem.com.utils.PageUtils;
import crmsystem.com.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import crmsystem.com.entity.AftersaleslogEntity;
import crmsystem.com.service.AftersaleslogService;

import javax.validation.Valid;


/**
 * 
 *
 * @author zhugaowei
 * @email 1912431877@qq.com
 * @date 2021-01-07 12:48:11
 */
@RestController
@RequestMapping("/aftersaleslog")
//@Api("AftersaleslogController相关api")
public class AftersaleslogController {
    @Autowired
    private AftersaleslogService aftersaleslogService;

    /**
     * 列表
     */
//    @ApiOperation("获取列表信息")
    @RequestMapping("/list")
    //@RequiresPermissions("com:aftersaleslog:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = aftersaleslogService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("com:aftersaleslog:info")
    public R info(@PathVariable("id") Integer id){
		AftersaleslogEntity aftersaleslog = aftersaleslogService.getById(id);

        return R.ok().put("aftersaleslog", aftersaleslog);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("com:aftersaleslog:save")
    public R save(@Valid @RequestBody AftersaleslogEntity aftersaleslog /*, BindingResult result*/){
//        if( result.hasErrors()){
//            Map<String,String> map=new HashMap<>();
//            //1.获取错误的校验结果
//            result.getFieldErrors().forEach((item)->{
//                //获取发生错误时的message
//                String message = item.getDefaultMessage();
//                //获取发生错误的字段
//                String field = item.getField();
//                map.put(field,message);
//            });
//            return R.error(400,"提交的数据不合法").put("data",map);
//        }else {
//
//        }
        aftersaleslogService.save(aftersaleslog);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("com:aftersaleslog:update")
    public R update(@Valid @RequestBody AftersaleslogEntity aftersaleslog){
		aftersaleslogService.updateById(aftersaleslog);

        return R.ok();
    }

    /**
     * 批量删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("com:aftersaleslog:delete")
    public R delete(@RequestBody Integer[] ids){
		aftersaleslogService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 根据id删除
     */
    @RequestMapping("/delete/{id}")
    //@RequiresPermissions("com:aftersaleslog:delete")
    public R deleteById(@PathVariable("id")Integer id){
        aftersaleslogService.removeById(id);
        return R.ok();
    }

    /**
     * 多字段模糊查询
     */
    @RequestMapping("/infoByIndex/{index}")
    //@RequiresPermissions("com:contactperson:info")
    public R infoByIndex(@PathVariable("index") String index){
        List<AftersaleslogEntity> entities = aftersaleslogService.getByIndex(index);
        return R.ok().put("entities", entities);
    }

}
